Inside Macintosh: Memory

Previous | Chapter Top | Chapter Contents | Next

Allocating Temporary Memory

In system software version 7.0 and later, you can manipulate temporary memory with three routines that are counterparts to other Memory Manager routines. The TempNewHandle function allocates a new block of relocatable memory, the TempFreeMem function returns the total amount of free memory available for temporary allocation, and the TempMaxMem function compacts the heap zone and returns the size of the largest contiguous block available for temporary allocation.

You should not call any of these memory-allocation routines at interrupt time.<36pt>

TempNewHandle

To allocate a new relocatable block of temporary memory, call the TempNewHandle function after making sure that there is enough free space to satisfy the request.

FUNCTION TempNewHandle (logicalSize: Size;
                                         VAR resultCode: OSErr): Handle;
logicalSize
The requested logical size, in bytes, of the new temporary block of memory.
resultCode
On exit, the result code from the function call.

DESCRIPTION

The TempNewHandle function returns a handle to a block of size logicalSize . If it cannot allocate a block of that size, the function returns NIL . Before you use the returned handle, make sure its value is not NIL .

ASSEMBLY-LANGUAGE INFORMATION

The trap macro and routine selector for TempNewHandle are

Trap macro

Selector

_OSDispatch

$001D

SPECIAL CONSIDERATIONS

Because TempNewHandle might allocate memory, you should not call it at interrupt time.

Note that TempNewHandle returns its result code in a parameter, not through MemError .

RESULT CODES

noErr

0

No error

memFullErr

-108

Not enough memory

TempFreeMem

To find out the total amount of memory available for temporary allocation, use the TempFreeMem function.

FUNCTION TempFreeMem: LongInt;

DESCRIPTION

The TempFreeMem function returns the total amount of free temporary memory that you could allocate by calling TempNewHandle . The returned value is the total number of free bytes. Because these bytes might be dispersed throughout memory, it is ordinarily not possible to allocate a single relocatable block of that size.

ASSEMBLY-LANGUAGE INFORMATION

The trap macro and routine selector for TempFreeMem are

Trap macro

Selector

_OSDispatch

$0018

SPECIAL CONSIDERATIONS

Even though TempFreeMem does not move or purge memory, you should not call it at interrupt time.

TempMaxMem

To find the size of the largest contiguous block available for temporary allocation, use the TempMaxMem function.

FUNCTION TempMaxMem (VAR grow: Size): Size;
grow
On exit, this parameter always contains 0 after the function call because temporary memory does not come from the application's heap zone, and only that zone can grow. Ignore this parameter.

DESCRIPTION

The TempMaxMem function compacts the current heap zone and returns the size of the largest contiguous block available for temporary allocation.

ASSEMBLY-LANGUAGE INFORMATION

The trap macro and routine selector for TempMaxMem are

Trap macro

Selector

_OSDispatch

$0015

SPECIAL CONSIDERATIONS

Because TempMaxMem could move memory, you should not call it at interrupt time.


© 1997 Apple Computer, Inc.

Previous | Chapter Top | Chapter Contents | Next